package com.bootdo.acitvity.DDD.domain.event;

import com.bootdo.acitvity.DDD.domain.CenterBatch;
import com.bootdo.acitvity.DDD.domain.entity.AdjuestmentStock;
import com.bootdo.acitvity.DDD.domain.entity.ShareStock;
import com.bootdo.acitvity.DDD.domain.entity.Stock;
import com.bootdo.acitvity.DDD.infrastrcture.exception.BizException;

/**
 *  更新共享库存事件
 */
public class StockAdjustmentStrategy implements StockUpdateStrategy  {

    @Override
    public void updateStock(CenterBatch batch, Stock stockChange) {
        AdjuestmentStock dto = (AdjuestmentStock) stockChange;
        ShareStock shareStockTobeUpdated = batch.getShareStock();

        /**
         *  调整出库数量不能大于可剩调整数量【共享库存】
         */
        boolean sharedStockEnough = shareStockTobeUpdated.isSharedStockEnough(shareStockTobeUpdated, dto);

        if(!sharedStockEnough){
            throw new BizException("库存不足");
        }

        ShareStock shareStockReduced = shareStockTobeUpdated.reduce(dto);

        batch.setShareStock(shareStockReduced);
    }
}
